﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Aspect;

namespace Nvigorate.Test.Aspect
{
    /// <summary>
    /// Summary description for LoggingAspectTests
    /// </summary>
    [TestClass]
    public class LoggingAspectTests
    {
        public LoggingAspectTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestClassLevelApplication()
        {
            TestLogAdapter.LoggedExceptions = 0;

            var test = new AllMethodsTest();

            InvokeInsideCatchBlock(test.CallPrivate);
            InvokeInsideCatchBlock(test.CallProtected);
            InvokeInsideCatchBlock(test.CallWithCatchAndThrow);
            InvokeInsideCatchBlock(test.CallWithCatchNoThrow);

            Assert.AreEqual(3, TestLogAdapter.LoggedExceptions);
        }

        [TestMethod, Ignore]
        public void TestMethodLevelApplication()
        {
            TestLogAdapter.LoggedExceptions = 0;

            var test = new IndividualMethodTest();

            InvokeInsideCatchBlock(test.AutoLogException);
            InvokeInsideCatchBlock(test.IndirectLogException);
            InvokeInsideCatchBlock(test.NoLogException);

            Assert.AreEqual(2, TestLogAdapter.LoggedExceptions);
        }

        public void InvokeInsideCatchBlock(Action function)
        {
            try
            {
                function.Invoke();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }

    [LoggingAspect(true)]
    public class AllMethodsTest
    {
        private void PrivateThrow()
        {
            try
            {
                throw new NullReferenceException("Exception");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        protected void ProtectedThrow()
        {
            try
            {
                throw new NullReferenceException("Exception");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void CallProtected()
        {
            ProtectedThrow();
        }

        public void CallPrivate()
        {
            PrivateThrow();
        }

        public void CallWithCatchAndThrow()
        {
            try
            {
                throw new NullReferenceException("Exception");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CallWithCatchNoThrow()
        {
            try
            {
                throw new Exception("Exception");
            }
            catch (Exception ex)
            {
            }
        }
    }

    public class IndividualMethodTest
    {
        [LoggingAspect(true)]
        public void AutoLogException()
        {
            throw new Exception("Exception");
        }

        public void IndirectLogException()
        {
            AutoLogException();
        }

        public void NoLogException()
        {
            throw new Exception("Test");
        }
    }

}
